import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenEquation;
import aurelienribon.tweenengine.TweenManager;
import aurelienribon.tweenengine.equations.Quart;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.lang.reflect.InvocationTargetException;

public class ButtonMove extends JFrame {

    private JButton bt;

    private TweenManager manager = new TweenManager();

    public ButtonMove() {
        JButton move = new JButton("move");
        move.setBounds(100, 20, 100, 30);
        move.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                doMove();
            }
        });
        bt = new JButton("ball");
        bt.setBounds(100, 200, 100, 30);
        this.setLayout(null);
        this.add(move);
        this.add(bt);
        this.setPreferredSize(new Dimension(800, 600));
        this.setSize(new Dimension(800, 600));
        /**
         * 向引擎注册该对象的实现
         */
        Tween.registerAccessor(JComponent.class, new JComponentAccessor());
        Tween.registerAccessor(Component.class, new ComponentAccessor());
        /**
         * 将一个Button对象在0.5秒钟的时间内移动到（100,100）的位置
         */

        run();
        this.setVisible(true);
    }

    private boolean running;

    private void doMove() {

        bt.setBounds(100, 200, 100, 30);

//        Tween.to(bt, ComponentAccessor.XY,
//                2.5f).target(500, 500)
//                .start(manager);
//        Tween.to(bt,ComponentAccessor.WH,3f).target(500,300)
//                .ease(Quart.IN)
//                .start(manager);
        bt.setBackground(Color.red);
//        Tween.to(bt,JComponentAccessor.ALPHA,6f).target(0,255)
//                .ease(Quart.OUT)
//                .start(manager)
//        ;

        Timeline.createParallel()
                .beginParallel()

                .push(Tween.to(bt,
                        ComponentAccessor.WH, 4.0f).
                        target(360, 200))
//                .push(Tween.to(bt,
//                                ComponentAccessor.ALPHA, 3.0f).
//                        target(0f, 255f))
                .end().start(manager);


    }

    private void run() {
        running = true;
        Runnable runnable = () -> {
            long lastMillis = System.currentTimeMillis();
            long deltaLastMillis = System.currentTimeMillis();

            while (running) {

                long newMillis = System.currentTimeMillis();

                long sleep = 15 - (newMillis - lastMillis);

                lastMillis = newMillis;


                if (sleep > 1) {

                    try {

                        Thread.sleep(sleep);

                    } catch (InterruptedException ex) {

                    }

                }

                long deltaNewMillis = System.currentTimeMillis();
                final float delta = (deltaNewMillis - deltaLastMillis) / 1000f;
                try {
                    //System.out.println("delta >>> "+delta);
                    SwingUtilities.invokeAndWait(() -> manager.update(delta));
                } catch (InterruptedException ex) {

                } catch (InvocationTargetException ex) {

                }
                deltaLastMillis = newMillis;
            }
        };

        new Thread(runnable).start();
    }

    public static void main(String[] args) {
        ButtonMove move = new ButtonMove();
        move.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
